package com.yyr.callmessage.img.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.yyr.callmessage.core.util.VerificationUtil;
import com.yyr.callmessage.img.domain.service.VerificationImgService;
import com.yyr.callmessage.user.object.qo.PhoneUserQo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @Description:  图片验证码模块
 * @Author：
 * @Date：2021/6/29
 */
@RestController
public class ImgController {

    @Resource
    private VerificationImgService verificationImgService;

    @Resource
    private DefaultKaptcha defaultKaptcha;


    /**
     * @api {get} /img 生成图片验证码
     * @apiName img_get
     * @apiGroup img
     *
     * @apiParam {String} phone
     *
     * @apiUse headerAndAuthorization
     * @apiUse successResponse
     */
    @GetMapping(value = "/img")
    public void getImgVerification(@RequestBody @Valid PhoneUserQo qo , HttpServletRequest request , HttpServletResponse response) throws Exception {
        String imgText  = VerificationUtil.validateCode(request , response , defaultKaptcha , qo.getPhone());
        verificationImgService.addVerificationImg(qo.getPhone() , imgText);
    }
}
